package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.TbParking;
import com.hqyj.dao.TbParkingMapper;
import com.hqyj.service.TbParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 车位表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-25
 */
@Service
public class TbParkingServiceImpl implements TbParkingService {

    @Autowired
    private TbParkingMapper parkingMapper;

    @Override
    public HashMap<String, Object> insertPark(TbParking tbParking) {
        HashMap<String,Object> map = new HashMap();
        List<TbParking> tbParkingList = parkingMapper.selectList(null);
        Iterator<TbParking> iterator = tbParkingList.iterator();
        //数据库中存在的code是否和参数中的code相同，
        //相同证明数据库有这个停车位，此时不能增加这个停车位
        int result = 0 ;
        while (iterator.hasNext()){
            //判断数据库中的code和community_id和传入参数中的两个是否一致，一致就不能新增车位
            TbParking parking = iterator.next();
            if(parking.getCode().equals(tbParking.getCode()) && parking.getCommunityId().equals(tbParking.getCommunityId()) ){
                result++;
                break;
            }
        }
        if(result!=0){
            map.put("info","所在小区车位编号重复");
        }else {
            int num = parkingMapper.insert(tbParking);
            if(num>0){
                map.put("info","保存成功");
            }else{
                map.put("info","保存失败");
            }
        }
      return map;
    }

    @Override
    public HashMap<String, Object> updatePark(TbParking tbParking) {
            HashMap<String,Object> map = new HashMap();
            //获取修改数据的当前时间
            tbParking.setUpdateTime(LocalDateTime.now());
            int num = parkingMapper.updateById(tbParking);
            if(num>0){
                map.put("info","保存成功");
            }else{
                map.put("info","保存失败");
            }
        return map;
    }

    @Override
    public HashMap<String, Object> deletePark(Integer id) {
        HashMap<String,Object>  map  = new HashMap<>();
        int num = parkingMapper.deleteById(id);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public  HashMap<String, Object> listPark(TbParking tbParking) {
        HashMap<String, Object> map = new HashMap<String,Object>();
        //第一步:创建分页对象
        Page<TbParking> tbParkingPage = new Page<>(tbParking.getPage(), tbParking.getRow());
       //创建条件查询对象
        QueryWrapper<TbParking> queryWrapper = new QueryWrapper<>();
        //第二步：查询分页，获取分页数据集
        Page<TbParking> pageList = parkingMapper.selectPage(tbParkingPage, queryWrapper);
        //第三步：
            // 获取当前页的条数，返回值是一个list
            map.put("info",pageList.getRecords());
            //获取总条数
            map.put("total",pageList.getTotal());
            //获取总页数
            map.put("totalPage",pageList.getPages());


        //查询所有的数据
//        List<TbParking> tbParkings = parkingMapper.selectList(null);
//        map.put("info",list);
          return map;
    }

    @Override
    public HashMap<String, Object> listCode() {
        HashMap<String, Object> map = new HashMap<String,Object>();
        //查询所有的数据
        List<TbParking> tbParkings = parkingMapper.selectList(null);
        //迭代所有的数据
        Iterator<TbParking> iterator = tbParkings.iterator();
        List<TbParking> list = new ArrayList<>();
        while(iterator.hasNext()){
            TbParking next = iterator.next();
            if(next.getName().equals("公有车位")){
                list.add(next);
            }
        }
        map.put("info",list);
        return map;
    }

    @Override
    public int updateNameAndUpdateTime(String code,String name, LocalDateTime updateTime) {
      return  parkingMapper.updateNameAndUpdateTime(code,name,updateTime);
    }
}
